﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Cashier_Utils;
using System.Collections;

namespace Cashier.pos
{
    public partial class DiscountForm : Form
    {
        private PosForm posform;
        private Decimal shipmentTotal = 0M; //出货单总金额
        private DiscountType discountType = DiscountType.Discount; //优惠方式
        private Decimal discount = 0M; //优惠数量
        private Decimal discountAmount = 0M; //优惠金额
        private bool isdiscount = false; //是否优惠

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="posform"></param>
        /// <param name="shipmentTotal">出货单总金额</param>
        /// <param name="discountType">优惠方式</param>
        /// <param name="discount">优惠数量</param>
        /// <param name="discountAmount">优惠金额</param>
        public DiscountForm(PosForm posform, decimal shipmentTotal, DiscountType discountType, decimal discount, decimal discountAmount)
        {
            InitializeComponent();
            this.posform = posform;
            
            this.shipmentTotal = shipmentTotal;
            this.discountType = discountType;
            this.discount = discount;
            this.discountAmount = discountAmount;
            if (this.discountType == DiscountType.Discount)
                radioButton_discount.Checked = true;
            else if (this.discountType == DiscountType.Less)
                radioButton_less.Checked = true;

            textBox_oldtotal.Text = shipmentTotal.ToString("f2");
            textBox_discount.Text = discount==0?"1":discount.ToString();
        }

        /// <summary>
        /// 优惠方式切换
        /// </summary>
        public void RadioExchange()
        {
            this.discountType = radioButton_discount.Checked ? DiscountType.Discount : DiscountType.Less;
            if (this.discountType == DiscountType.Discount)
            {
                this.textBox_discount.Text = "1";
                label_typename.Text = "折扣：";
                label_discount.Text = "如输入0.9则打9折，1为不打折";
            }
            else
            {
                this.textBox_discount.Text = "0";
                label_typename.Text = "直减：";
                label_discount.Text = "直减金额不得超过原合计金额";
            }
        }

        /// <summary>
        /// 单选按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            this.RadioExchange();
            textBox_discount.Focus();
        }

        /// <summary>
        /// 判断优惠是否合法
        /// </summary>
        /// <param name="discountAmount">优惠金额</param>
        /// <returns></returns>
        private bool IsLawDiscount()
        {
            bool islaw = false;
            String discountString = textBox_discount.Text.Trim();
            String re = @"^\d+(\.\d+)?$";

            if (Regex.IsMatch(discountString, re))
            {
                this.discount = Decimal.Parse(discountString);
                if (this.discountType == DiscountType.Discount && this.discount <= 1)
                {
                    if (this.discount == 0)
                        this.discount = 1;
                    this.discountAmount = this.shipmentTotal - this.shipmentTotal * this.discount;
                    islaw = true;
                }
                else if (this.discountType == DiscountType.Less && this.discount <= this.shipmentTotal)
                {
                    this.discountAmount = this.discount;
                    islaw = true;
                }
            }
            return islaw;
        }

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            if (keyData == Keys.Enter)
            {
                isdiscount = true;
                if (!button_ok.Focused)
                    this.Close();
            }
            else if (keyData == Keys.Escape)
            {
                isdiscount = false;
                this.Close();
            }
            else if (keyData == Keys.F5)
                radioButton_discount.Checked = true;
            else if (keyData == Keys.F6)
                radioButton_less.Checked = true;
            return b;
        }

        /// <summary>
        /// TextBox键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            int keyValue = (int)e.KeyChar;
            if ((keyValue > 47 && keyValue < 58) || keyValue == 8 || keyValue == 46)
            {
                e.Handled = false;
            }
            else if (keyValue == 13)
                e.Handled = false;
            else
                e.Handled = true;
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DiscountForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isdiscount && !this.IsLawDiscount())
            {
                e.Cancel = true;
                isdiscount = false;
            }

            if (isdiscount)
            {
                this.posform.ShipmentDiscount(this.discountType, discount, discountAmount);
            }
        }

        /// <summary>
        /// 确定按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender, EventArgs e)
        {
            isdiscount = true;
            this.Close();
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            isdiscount = false;
            this.Close();
        }
    }
}
